<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="../css/zhuce.css">
</head>

<body>
    <div class="top">
        <div class="left">
            <a href="../index.html"><img src="https://book.easou.com/jsp/online/resources/imgs/return.png" alt=""></a>
        </div>
        <div class="zhong">注册</div>
        <div class="right">
            <a href="../index.html"><img src="https://book.easou.com/jsp/online/resources/imgs/home.png" alt=""></a>
        </div>
    </div>
    <div class="zhuce">
        <div class="shuru">
            <input type="text" class="username" placeholder="请输入手机号" name="phone">
        </div>
        <div class="shuru">
            <input type="password" class="password0" placeholder="请设置密码(6-20英文字符、数字)" name="password">
        </div>
        <div class="shuru">
            <input type="password" class="password1" placeholder="请再次确认密码">
        </div>
        <div class="denglu cf"><a href="./denglu.html" class="register">登录</a></div>
        <div class="btn" id="btn">
            注册
        </div>
    </div>

</body>
<script src="../js/jquery.min.js"></script>
<script>
    $("#btn").on("click", function () {
        if ($(".username").val().length <= 0) {
            alert("用户名不能为空哦，请再检查一下！");
            return;
        } else {
            let regex = /^1[3456789]\d{9}$/;//使用此正则即可
            let value = $(".username").val();
            if (!regex.test(value)) {
                alert("请输入正确的手机号，请再检查一下！");
                return;
            }
        }
        if ($(".password0").val() != $(".password1").val()) {
            alert("两次输入密码不一致，请再检查一下！");
            return;
        }
        if ($(".password0").val().length < 6) {
            alert("密码个数不能小于6位哦，请再检查一下！");
            return;
        }
        if ($(".password0").val().length > 20) {
            alert("密码个数不能大于20位哦，请再检查一下！");
            return;
        }
       
            $.ajax({
            type: "get",
            url: "http://localhost:3000/dengSearch",
            data: {
                phone: $(".username").val(),
            },
            success: function (response) {
                if (response.data.length > 0) {
                    alert("该手机已经注册，亲登录")
                    setTimeout(() => {
                        window.open("./denglu.html", "_self")
                    }, 1000)
                } else {
                    $.ajax({
                        type: "post",
                        url: "http://localhost:3000/zhuceSearchAll",
                        data: {
                            phone: $(".username").val(),
                            password: $(".password0").val()
                        },
                        success: function (res) {
                            alert("注册成功,请登录");
                            setTimeout(()=>{
                                window.open("./denglu.html", "_self")
                            },1000)
                        }
                    });
                }
            }
        })
    });
</script>

</html>